package com.xc.stock.domain.basic.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ag.exception.EntityNotFoundException;
import com.ag.utils.AgAssert;
import com.ag.utils.NumberUtils;
import com.xc.stock.domain.basic.entity.UserStaff;
import com.xc.stock.domain.basic.repository.UserStaffRepository;
import com.xc.stock.utils.SpringUtils;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * AppVersionService
 *
 * @author xlfd
 * @email xlfd@gmail.com
 * @created 2021/10/25
 */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class UserStaffService {
	final UserStaffRepository repository;
//	static QUserStaff q = QAppVersionPO.appVersionPO;

	public UserStaff create(UserStaff cmd) {
		repository.save(cmd);
		return cmd;
	}
	
	public UserStaff modify(UserStaff cmd) {
		UserStaff userStaff = repository.find(cmd.getId());
		AgAssert.notNull(userStaff, EntityNotFoundException.class, "找不到信息");
		userStaff.update(cmd);
		repository.save(userStaff);
		return userStaff;
	}
	
	public String findUserStaffServiceAddr(Integer agentId) {
		UserStaff userStaff = null;
		if(NumberUtils.isValid(agentId)) {
			userStaff = repository.findUserStaff(agentId); 
		}
		
		if(userStaff == null) {
			userStaff = repository.findUserStaff(0);
		}
		
		//都没配置
		if(userStaff == null) {
			log.warn("没有配置默认值");
			return "";
		}
		
		return userStaff.getServiceAddr();
	}
	
	public UserStaffRepository repository() {
		return repository;
	}

	public void remove(Integer id) {
		repository.deleteById(id);
	}
	
	public static UserStaffService of() {
		return SpringUtils.getBean(UserStaffService.class);
	}
}
